#include <iostream>
#include <string>


using namespace std;

class Solution {
public:
    string decodeString(string s) {
        if (s.size() == 0) return s;
        if (isalpha(s[0])) return s[0] + decodeString(s.substr(1));

        int i = s.find('[');
        int k = stoi(s.substr(0, i));
        int balance = 1;
        for (int j = i + 1; j < s.size(); ++j) {
            if (s[j] == '[') ++balance;
            else if (s[j] == ']') {
                balance--;
                if (balance == 0) {
                    string t = decodeString(s.substr(i + 1, j - i - 1));
                    string res;
                    while (k--) res += t;
                    return res + decodeString(s.substr(j + 1));
                }
            }
        }
        return "";
    }
};

int main() {
    Solution sol;
    string s = sol.decodeString("3[ac]");
}